package com.unitechs.trap.excel.webapi.service.impl;

import com.unitechs.trap.common.pojo.trap.entity.VarData;
import com.unitechs.trap.excel.service.IReadVarDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
@Slf4j
public class ReadVarDataServiceImpl implements IReadVarDataService {

    /**
     * 从excel获取变量数据并赋值
     * @param url
     * @return
     */
    public List<VarData> readVarData_C(String url, String version) {

        List<VarData> list = new ArrayList<>();
        VarData lastData = new VarData();
        try {
            File file = new File(url);
            File[] files = file.listFiles();
            for (File f : files) {
                if (!f.isDirectory() && f.getName().endsWith(".xls")) {
                    //创建工作簿对象
                    HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(f.getPath()));
                    //读取第1个工作表
                    HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    //获取最后一行的num，即总行数。此处从0开始
                    int maxRow = sheet.getLastRowNum();
                    for (int row = 1; row <= maxRow; row++) {
                        VarData varData = new VarData();

                        if (sheet.getRow(row).getCell(VarData.getVarOidCode(version)) == null
                                || sheet.getRow(row).getCell(VarData.getVarOidCode(version)).toString().equals("")) {
                            continue;
                        }
                        varData.setVarOid(sheet, row, version);
                        varData.setVarName(sheet, row, version);
                        varData.setTrapOid(lastData, sheet, row, version);
                        varData.setVarDesc(varData, lastData, sheet, row, version);
                        varData.setVarValue(varData, lastData, sheet, row, version);
                        lastData = varData;
                        list.add(varData);
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }
    /**
     * 从excel获取变量数据并赋值
     * @param url
     * @return
     */
    public List<VarData> readVarData_D(String url, String version) {

        List<VarData> list = new ArrayList<>();
        VarData lastData = new VarData();
        try {
            File file = new File(url);
            File[] files = file.listFiles();
            for (File f : files) {
                if (!f.isDirectory() && f.getName().endsWith(".xls")) {
                    //创建工作簿对象
                    HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(f.getPath()));
                    //读取第1个工作表
                    HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    //获取最后一行的num，即总行数。此处从0开始
                    int maxRow = sheet.getLastRowNum();
                    for (int row = 1; row <= maxRow; row++) {
                        VarData varData = new VarData();

                        if (sheet.getRow(row).getCell(VarData.getVarOidCode(version)) == null
                                || sheet.getRow(row).getCell(VarData.getVarOidCode(version)).toString().equals("")) {
                            continue;
                        }
                        varData.setVarOid(sheet, row, version);
                        varData.setVarName(sheet, row, version);
                        varData.setTrapOid(lastData, sheet, row, version);
                        varData.setVarDesc(varData, lastData, sheet, row, version);
                        varData.setVarValue(varData, lastData, sheet, row, version);
                        lastData = varData;
                        log.info("vardata{}",varData);
                        list.add(varData);
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<VarData> readVarDataByVersion(String url, String coName,String version) {

        List<VarData> list=new ArrayList<>();
        switch (version){
            case "A": break;
            case "B": break;
            case "C": list=readVarData_C(url, version);
            log.info("进入了readVarDataByVersion的C版本");
            break;
            case "D": list=readVarData_D(url, version);
            log.info("进入了readVarDataByVersion的D版本");
            break;
        }
        return list;
    }
}
